package core.tech.utilies;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.RandomAccess;

public class MyList<E> extends ArrayList<E> implements List<E>, RandomAccess,
		Cloneable, java.io.Serializable {
	private static final long serialVersionUID = -8801962734639017349L;

	/**
	 * Checks if this <tt>ArrayList</tt> instance contains the given object. An
	 * application can use this operation to view if this instance has a
	 * specified element. <tt>Returns</tt> TRUE if present and FALSE if not.
	 */
	public boolean hasElement(Object obj) {
		for (Object o : super.toArray()) {
			if (o.equals(obj)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Prints all the contents of this <tt>ArrayList</tt> instance. An
	 * application can use this operation to view all the contents of this
	 * <tt>ArrayList</tt> instance at any moment.
	 */
	public void printAll() {
		for (Object o : super.toArray()) {
			System.out.println(o.toString());
		}
	}

	/**
	 * Gets all the entries of this <tt>ArrayList</tt> instance. An application
	 * can use this operation to get all the contents of this <tt>ArrayList</tt>
	 * instance at any moment.
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<E> getAll() {
		ArrayList<E> l = new ArrayList<E>();
		for (Object o : super.toArray()) {
			l.add((E) o);
		}
		return l;
	}

	/**
	 * Removes the duplicate entries from this <tt>ArrayList</tt> instance, but
	 * the order is not maintained. An application can use this operation to
	 * eliminate the duplicate entries from this <tt>ArrayList</tt> instance,
	 * without maintaining the order.
	 */
	public void removeDuplicates() {
		HashSet<E> set = new HashSet<E>(this.getAll());
		this.clear();
		this.addAll(set);
	}

	/**
	 * Removes the duplicate entries from this <tt>ArrayList</tt> instance by
	 * maintaining the order. An application can use this operation to eliminate
	 * the duplicate entries from this <tt>ArrayList</tt> instance with the
	 * order maintained.
	 */
	public void eliminateDuplicates() {
		ArrayList<E> list1 = this.getAll();
		MyList<E> list2 = new MyList<E>();
		for (E o : list1) {
			if (!list2.toString().contains(o.toString())) {
				list2.add(o);
			}
		}
		this.clear();
		this.addAll(list2);
	}
}